﻿<?php
include_once("./function.php");
//print_r($_GET);
$sql="select * from ts_product_cate";
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	if((mysqli_num_rows($res))>0)
	{
		$pro=array();
		while($list=mysqli_fetch_assoc($res))
		{
			$pro[]=$list;
		}
		//print_r($pro);die;//Array ( [0] => Array ( [pc_id] => 1 [pc_fid] => 0 [pc_name] => 服装 [pc_level] => 1 [pc_ifshow] => noshow )
	}
}
$sql="select * from ts_product where pid=$_GET[pid]";
$res=mysqli_query($link,$sql);
if(is_object($res))
{
	$one=mysqli_fetch_assoc($res);
	
}
//print_r($one);
//无线分类
function card($arr,$pc_fid)
{
	static $new=array();
	foreach($arr as $v)
	{
		if($v['pc_fid']==$pc_fid)
		{
			$new[]=$v;
			card($arr,$v['pc_id']);
		}
	}
	return $new;
}
$pro=card($pro,0);	
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>商品修改</title>
<base target="_self">
<link rel="stylesheet" type="text/css" href="skin/css/base.css" />
<link rel="stylesheet" type="text/css" href="skin/css/main.css" /> 
<style>.sp{display:inline-block;} .sp_color{width:35px;display:inline-block;padding:0px;border:0px;background:#fff;}
.cover{width:100%;height:100%;background:#000;position:fixed;top:0;left:0;opacity:0.2;}
.bigimg{width:600px;height:400px;position:fixed;left:50%;top:50%;margin-top:-200px;margin-left:-300px;padding:15px;}
</style>
</head>
<body leftmargin="8" topmargin='8' id="bd">
<form action="do_product_edit.php" method="post" id="form" onsubmit="return check();" enctype="multipart/form-data">
<table width="98%" align="center" border="0" cellpadding="3" cellspacing="1" bgcolor="#CBD8AC" style="margin-bottom:8px;margin-top:8px;">
  <tr>
    <td background="skin/images/frame/wbg.gif" bgcolor="#EEF4EA" class='title' colspan="2"><span><img src='skin/images/frame/arr3.gif' style='margin-right:10px;'>修改商品</span></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td>
		<table style="margin-bottom:8px;margin-top:8px;margin-left:20px;">
		
		<tr>
			<td>商品名称:</td>
			<td>
				<input type="text" name="pname" value="<?=$one['pname']?>"/><font color="red">*</font>
				<span id="tip_u" class="sp"></span>
			
			</td>	
		</tr>
		
			 <tr>
				<td width="100px;">商品分类:</td>
				<td>
				<select name="id">
					<option value="0">顶级分类</option>
					<?php foreach($pro as $v){?>
						<option value="<?=$v['pc_id']?>"
					<?php 
					$sql="select * from ts_product_cate where pc_fid=".$v["pc_id"];
					$res=mysqli_query($link,$sql);
					if((mysqli_num_rows($res))>0){?>disabled
					<?php }?>
					<?=$one['p_c_id']==$v['pc_id']?'selected':''?>
					><?=str_repeat("&nbsp;",3*($v['pc_level']-1)).$v['pc_name']?></option>
					<?php }?>
			</select><font color="red">*</font>
					<span id="tip_f" class="sp">
				</td>
			  </tr>
			   <tr>
				 <td>产地:</td>
				 <td><input type="text" name="addr" value="<?=$one['addr']?>"/><font color="red">*</font><span id="tip_a" class="sp"></span></td>
			   </tr>
			   <tr>
				 <td>商品价格:</td>
				 <td><input type="text" name="price" value="<?=$one['price']?>"/><font color="red">*</font><span id="tip_p" class="sp"></span></td>
			   </tr>
			   <tr>
				 <td>优惠价格:</td>
				 <td><input type="text" name="yh" value="<?=$one['sprice']?>"/><font color="red">*</font><span id="tip_y" class="sp"></span></td>
			   </tr>
			   <tr>
				 <td>关键字:</td>
				 <td><input type="text" name="keywords" value="<?=$one['keyword']?>"/></td>
			   </tr>
			   <tr>
				 <td>商品编号:</td>
				 <td><input type="text" name="bh" value="<?=$one['pnums']?>"/></td>
			   </tr>
			   <tr>
				 <td>库存:</td>
				 <td><input type="text" name="kc" value="<?=$one['snums']?>"/></td>
			   </tr>
			   <tr>
				 <td>上架/下架</td>
				 <td><input type="radio" name="sj" value="<?=$one['ifshow']?>" checked>上架 <input type="radio" name="sj" value="<?=$one['ifshow']?>"> 下架</td>
			   </tr>
			   <tr>
				 <td>商品颜色</td>
				 <td>
					<span id="first">
					<span id="spa" onclick="create();" style="font-size:20px;">+</span>

					<?php $a=explode(',',$one['color']);foreach($a as $v=>$k){?>
					<span onclick="del()" style='font-size:25px;'>-</span>
					<input type="color" name='color[]' style='width:35px;padding:0px;border:0px;background:#fff;' value="<?=$a[$v]?>">
					<?php }?>

					</span>
					
				</td>
			   </tr>
			   <tr>
				 <td>商品尺码:</td>
				 <td><input type="checkbox" name="size[]" value="XS" <?php $arr=explode(',',$one['size']); if(in_array('XS',$arr)){?>checked<?php }?>> XS 
					 <input type="checkbox" name="size[]" value="S" <?php if(in_array('S',$arr)){?>checked<?php }?> onclick="choose(this)"> S
					 <input type="checkbox" name="size[]" value="M" <?php if(in_array('M',$arr)){?>checked<?php }?>
					 onclick="choose(this)"> M
					 <input type="checkbox" name="size[]" value="L" <?php if(in_array('L',$arr)){?>checked<?php }?>
					 onclick="choose(this)"> L
					 <input type="checkbox" name="size[]" value="XL" <?php if(in_array('XL',$arr)){?>checked<?php }?>onclick="choose(this)"> XL
					 <input type="checkbox" name="size[]" value="XXL" <?php if(in_array('XXL',$arr)){?>checked<?php }?>onclick="choose(this)"> XXL
					 <input type="checkbox" name="size[]" value="XXXL" <?php if(in_array('XXXL',$arr)){?>checked<?php }?>onclick="choose(this)"> XXXL
					 <span id="tip_s" class="sp"></span>
				 </td>
			   </tr>
			   <tr>
					<td>标题图片:</td>
					<td><input type="file" name="bimg" value="">
					<?php if(!empty($one['thumb'])){?>
					<img src="<?=$one['thumb']?>" width="25px" height="25px" style="margin-left:10px;" onclick="grtBig(this)">
					<?php } ?>
					</td>
			   </tr>
			   <tr>
					<td>商品图册:</td>
					<td>
					<span id="second">
					<span id="spa" onclick="create1();" style="font-size:20px;">+</span>
					<input type="file" name="simg[]" value="<?=(isset($b))?$b[$v]:''?>"/>
					<?php $b=explode(',',$one['pimgs']); foreach($b as $v=>$k){if(!empty($one['pimgs'])){?>
					<img src="<?=$b[$v]?>" width="25px" height="25px" style="margin-left:10px;"onclick="grtBig(this)"/>
					<?php }}?>
					</span>
					</td>
			   </tr>
			   <tr>
					<td>商品描述:</td>
					<td>    <?php
              include_once("../fckeditor/fckeditor.php");
              $fckeditor = new FCKeditor("introduce");//定义默认值 name
              $fckeditor->Width = "260px";//定义编辑器的宽度
              $fckeditor->Height = "130px";//定义编辑器的高度
              $fckeditor->Value = $one['descp'];//定义默认值
              $fckeditor->BasePath='../fckeditor/';
              $fckeditor->ToolbarSet = "Basic";
              $fckeditor->Create();//创建编辑器
            ?></td>
			   </tr>
			   <tr>
				 <td colspan="2" align="left" height="40px">
				 <input type="hidden" name="pid" value="<?=$_GET['pid']?>"/>
					<input type="submit" name="sub" value="修改" style="background:#ffffcc;margin-left:100px;"/>
				 </td>
			   </tr>
		</table>
	</td>
  </tr>
</table>
</form>
<script type="text/javascript">
	tip_u=document.getElementById("tip_u");
	tip_f=document.getElementById("tip_f");
	tip_a=document.getElementById("tip_a");
	tip_s=document.getElementById("tip_s");
	tip_p=document.getElementById("tip_p");
	function getValue(obj,tip)
	{
		if(obj.value=="")
		{
			tip.style.color="red";
			tip.innerHTML="不能为空";
		}
		else
		{
			tip.innerHTML="";
		}
	}
	form.pname.onblur=function()
	{
		getValue(form.pname,tip_u);
	}

	form.addr.onblur=function()
	{
		getValue(form.addr,tip_a);
	}
  // tip_p=document.getElementById("tip_p");
	form.price.onblur=function()
	{
		if(form.price.value=="")
		{
			tip_p.style.color="red";
			tip_p.innerHTML="不能为空";
		}
		else
		{
			 if(!isNaN(form.price.value))
			 {
				tip_p.innerHTML="";
			 }
			 else
			 {  
			    tip_p.style.color="red";
				tip_p.innerHTML="价格书写不对";
			 }
			
		}
	}
	tip_y=document.getElementById("tip_y");
	form.yh.onblur=function()
	{
		 if(!isNaN(form.yh.value))
			{
				tip_y.innerHTML="";
				if(parseInt(form.yh.value)>parseInt(form.price.value))
				 {
					tip_y.style.color="red";
					tip_y.innerHTML="优惠价格需小于等于原价";
				 }
			}
			else
			 {  
				tip_y.style.color="red";
				tip_y.innerHTML="优惠价格书写不对";
			 }  
	}
	
size=document.getElementsByName("size[]");
function choose(a)
{
	checkNum=0;
	for(let i=0;i<size.length;i++)
	{
		if(size[i].checked==true)
		{
			checkNum++;
		}
	}
	if(checkNum==0)
	{
		a.checked=true;
	}
}
first=document.getElementById("first");
	function create()
	{
		//以下节点的创建内容包裹在下面创建的div中
		span=document.createElement('span');
		span.setAttribute('onclick','del();');
		span.setAttribute('style','font-size:25px;');
		d=document.createTextNode('-');
		span.appendChild(d);
		first.appendChild(span);
		//元素节点的创建
		inp=document.createElement('input');
		inp.setAttribute('type','color');
		inp.setAttribute('name','color[]');
		inp.setAttribute('style','width:35px;padding:0px;border:0px;background:#fff;')
		first.appendChild(inp);
	}
	function del()
	{
		first.removeChild(first.lastChild);
	}
	second=document.getElementById("second");
	function create1()
	{
		//以下节点的创建内容包裹在下面创建的div中
		br=document.createElement('br');        
		second.appendChild(br);
		span=document.createElement('span'); 
		span.setAttribute('onclick','del2();');
		span.setAttribute('style','font-size:25px;');
		d=document.createTextNode('-');
		span.appendChild(d);
		second.appendChild(span);
		//元素节点的创建
		inp=document.createElement('input');
		inp.setAttribute('type','file');
		inp.setAttribute('name','simg[]');
		//inp.setAttribute('style','width:35px;padding:0px;border:0px;background:#fff;')
		second.appendChild(inp);
	}
	function del2()
	{
		second.removeChild(second.lastChild);
	}
	function check()
	{
		if(form.pname.value==""||form.addr.value==""||form.price.value==""||form.bimg.value=="")
		{
			alert("修改的商品名称,商品价格,产地和标题图不能为空");
			return false;
		}
		return true;
	}
	var bd=document.getElementById("bd");
	function grtBig(picObj)
	{
		obj_cover=document.createElement("div");
		obj_cover.setAttribute('class','cover');
		bd.appendChild(obj_cover);
		obj_img=document.createElement("img");
		obj_img.setAttribute('src',picObj.src);
		obj_img.setAttribute('class','bigimg');
		obj_img.setAttribute('onclick','getBack()');
		bd.appendChild(obj_img);
	}
	//返回原图
	function getBack()
	{
		bd.removeChild(obj_img);//删除图像节点
		bd.removeChild(obj_cover);//删除遮罩层
	}
</script>
</body>
</html>

